﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
using OpenTK;

namespace MyRTS
{
    public partial class Form1 : Form
    {
        public void RenderVBO()
        {
            viewMatrix = viewMatrix;
            GL.Color4(Color.Red);
            GL.Begin(BeginMode.Lines);
          
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 10, 0);
         //   GL.Color4(Color.Green);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(10,0, 0);
          //  GL.Color4(Color.Green);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0,0,10);
            GL.End();

           // viewMatrix = Matrix4.Identity;
       //     Matrix4 rotx = Matrix4.CreateRotationX(xangle);
       //     Matrix4.Mult(ref rotx, ref viewMatrix, out viewMatrix);
   ///         Matrix4 roty = Matrix4.CreateRotationY(yangle);
    //        Matrix4.Mult(ref roty, ref viewMatrix, out viewMatrix);
     //       Matrix4 rotz = Matrix4.CreateRotationZ(zangle);
     //       Matrix4.Mult(ref rotz, ref viewMatrix, out viewMatrix);
          //  viewMatrix = Matrix4.Identity;
            
          //  Matrix4.Mult(ref camrot, ref viewMatrix, out viewMatrix);
        //    Matrix4.Mult(ref campos, ref viewMatrix, out viewMatrix);
            viewMatrix = Matrix4.LookAt(camposp, camposp+camtarget, camup);
            dispalymatrix(viewMatrix);

            /*
            if (scene != null)
            {
                foreach (Model m in scene.ModelsList)
                {
                    /*
                    GL.BindVertexArray(m.vaobuffor);
                    GL.EnableClientState(EnableCap.VertexArray);
                    GL.EnableClientState(EnableCap.NormalArray);
                 //   GL.EnableClientState(EnableCap.TextureCoordArray);
                    GL.EnableClientState(EnableCap.ColorArray);
                  //  GL.BindBuffer(BufferTarget.ArrayBuffer, m.bufforNo);
                    GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(m.Vertices), new IntPtr(0));
                    GL.NormalPointer(NormalPointerType.Float, BlittableValueType.StrideOf(m.Vertices), (IntPtr)(3 * sizeof(float)));
              //      GL.TexCoordPointer(2, TexCoordPointerType.Float, BlittableValueType.StrideOf(m.Vertices), new IntPtr(24));
                    GL.ColorPointer(4, ColorPointerType.Float, BlittableValueType.StrideOf(m.Vertices), (IntPtr)(8 * sizeof(float)));

                    
                    GL.DrawArrays(BeginMode.Triangles, 0, m.Vertices.Length);
                    GL.BindVertexArray(0);
                 //   GL.Finish(); 
                    GL.DisableClientState(EnableCap.ColorArray);
                    GL.DisableClientState(EnableCap.VertexArray);
                    GL.DisableClientState(EnableCap.NormalArray);
                     
                    modelMatrix = Matrix4.Identity;

                 //   Matrix4.Mult(ref rotation, ref Matrix4.Identity, out modelMatrix);
                   Matrix4 yrot= Matrix4.CreateRotationY(m.yangle);
                   Matrix4 xrot = Matrix4.CreateRotationY(m.xangle);
                   Matrix4 zrot = Matrix4.CreateRotationY(m.zangle);
                    Matrix4 tra = Matrix4.CreateTranslation(m.translate);
                 
                    Matrix4.Mult(ref tra, ref modelMatrix, out modelMatrix);
                   
                    Matrix4.Mult(ref xrot, ref modelMatrix, out modelMatrix);
                    Matrix4.Mult(ref yrot, ref modelMatrix, out modelMatrix);
                    Matrix4.Mult(ref zrot, ref modelMatrix, out modelMatrix);

                    Matrix4.Mult(ref modelMatrix, ref viewMatrix, out modelviewMatrix);
                    
                 //   label2.Text=viewMatrix.M11.ToString() + "   " + viewMatrix.M21.ToString() + "  " + viewMatrix.M31.ToString();
                 //   label3.Text = viewMatrix.M12.ToString() + "   " + viewMatrix.M22.ToString() + "  " + viewMatrix.M32.ToString();
                 //   label4.Text = viewMatrix.M13.ToString() + "   " + viewMatrix.M23.ToString() + "  " + viewMatrix.M33.ToString();


                  //  Matrix4.Mult(ref rotation, ref modelviewMatrix, out modelviewMatrix);
                    GL.UniformMatrix4(modelviewMatrixLocation, false, ref modelviewMatrix);
                    GL.BindVertexArray(m.vaoHandle);
                    GL.DrawElements(BeginMode.Triangles, m.vertexIndex.Length,
                        DrawElementsType.UnsignedInt, IntPtr.Zero);
                    GL.BindVertexArray(0);


                }
            }
    */

            if (newscene != null)
            {
                foreach (NewModel m in newscene.NewModelList)
                {
                    /*
                    GL.BindVertexArray(m.vaobuffor);
                    GL.EnableClientState(EnableCap.VertexArray);
                    GL.EnableClientState(EnableCap.NormalArray);
                 //   GL.EnableClientState(EnableCap.TextureCoordArray);
                    GL.EnableClientState(EnableCap.ColorArray);
                  //  GL.BindBuffer(BufferTarget.ArrayBuffer, m.bufforNo);
                    GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(m.Vertices), new IntPtr(0));
                    GL.NormalPointer(NormalPointerType.Float, BlittableValueType.StrideOf(m.Vertices), (IntPtr)(3 * sizeof(float)));
              //      GL.TexCoordPointer(2, TexCoordPointerType.Float, BlittableValueType.StrideOf(m.Vertices), new IntPtr(24));
                    GL.ColorPointer(4, ColorPointerType.Float, BlittableValueType.StrideOf(m.Vertices), (IntPtr)(8 * sizeof(float)));

                    
                    GL.DrawArrays(BeginMode.Triangles, 0, m.Vertices.Length);
                    GL.BindVertexArray(0);
                 //   GL.Finish(); 
                    GL.DisableClientState(EnableCap.ColorArray);
                    GL.DisableClientState(EnableCap.VertexArray);
                    GL.DisableClientState(EnableCap.NormalArray);
                     */
                    modelMatrix = Matrix4.Identity;

                       Matrix4.Mult(ref rotation, ref Matrix4.Identity, out modelMatrix);
                //    Matrix4 yrot = Matrix4.CreateRotationY(m.yangle);
                //    Matrix4 xrot = Matrix4.CreateRotationY(m.xangle);
               //     Matrix4 zrot = Matrix4.CreateRotationY(m.zangle);
                //    Matrix4 tra = Matrix4.CreateTranslation(m.translate);

               //     Matrix4.Mult(ref tra, ref modelMatrix, out modelMatrix);

               //     Matrix4.Mult(ref xrot, ref modelMatrix, out modelMatrix);
               //     Matrix4.Mult(ref yrot, ref modelMatrix, out modelMatrix);
              //      Matrix4.Mult(ref zrot, ref modelMatrix, out modelMatrix);

                    Matrix4.Mult(ref modelMatrix, ref viewMatrix, out modelviewMatrix);

                    //   label2.Text=viewMatrix.M11.ToString() + "   " + viewMatrix.M21.ToString() + "  " + viewMatrix.M31.ToString();
                    //   label3.Text = viewMatrix.M12.ToString() + "   " + viewMatrix.M22.ToString() + "  " + viewMatrix.M32.ToString();
                    //   label4.Text = viewMatrix.M13.ToString() + "   " + viewMatrix.M23.ToString() + "  " + viewMatrix.M33.ToString();


                    //  Matrix4.Mult(ref rotation, ref modelviewMatrix, out modelviewMatrix);
                    GL.UniformMatrix4(modelviewMatrixLocation, false, ref modelviewMatrix);
                    GL.BindVertexArray(m.vaoHandle);
                   GL.DrawElements(BeginMode.Triangles, m.VertexIndex.Count,
                        DrawElementsType.UnsignedInt, IntPtr.Zero);

                    
                    GL.BindVertexArray(0);


                }
            }


        }


        public void dispalymatrix(Matrix4 m)
        {
            table1.Clear();
            table1.Rows.Add(m.M11, m.M12, m.M13, m.M14);
            table1.Rows.Add(m.M21, m.M22, m.M23, m.M24);
            table1.Rows.Add(m.M31, m.M32, m.M33, m.M34);
            table1.Rows.Add(m.M41, m.M42, m.M43, m.M44);

        }
    }
}